package xyz.xtt.asset.script;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.constants.AssetCache;
import xyz.xtt.asset.entity.TblAsset;
import xyz.xtt.asset.mapper.TblAssetMapper;
import xyz.xtt.asset.service.ITblAssetService;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.common.starter.cache.CacheManager;

@Slf4j
@Schema(name = "获取资产信息", description = "资产详细信息")
@RequestMapping("/api/asset/script")
@RestController
public class AssetChangeController {

	@Autowired
	private TblAssetMapper tblAssetMapper;

	@Autowired
	private ITblAssetService tblAssetService;

	@Autowired
	private CacheManager cacheManager;

	/**
	 * 资产信息表处理脚本
	 */
	@Operation(summary = "资产信息表处理脚本", description = "用于查找最顶级父类ID")
	@GetMapping("/assetTabDisposeScript")
	public ApiResult<?> assetTabDisposeScript() {
		new Thread(this::recursionFindAssetPage).start();
		return ApiResult.ok();
	}

	public void recursionFindAssetPage() {
		int batchSize = 2000;
		// 递归查询总数据，一次处理1000条
		LambdaQueryWrapper<TblAsset> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.select(TblAsset::getId, TblAsset::getParentId, TblAsset::getGoodsId, TblAsset::getGoodsId, TblAsset::getRootAssetId);
		queryWrapper.eq(TblAsset::getRootAssetId, 0).orderByDesc(TblAsset::getId);
		Page<TblAsset> tblAssetPage = tblAssetMapper.selectPage(new Page<>(1, batchSize), queryWrapper);
		if (tblAssetPage.getTotal() == 0) {
			return;
		}
		List<TblAsset> records = tblAssetPage.getRecords();
		// 用于更新的结果集
		List<TblAsset> sortedTblAssets = new ArrayList<>();
		for (TblAsset tblAsset : records) {
			// 递归查找形成根资产id
			Long rootAssetId = recursionFindRootAssetId(tblAsset);
			tblAsset.setRootAssetId(rootAssetId).setLastModifyDatetime(System.currentTimeMillis());
			sortedTblAssets.add(tblAsset);
		}

		tblAssetService.batchDisposeScriptRestList(sortedTblAssets, batchSize);

		for (TblAsset tblAsset : sortedTblAssets) {
			cacheManager.delete(AssetCache.TBL_ASSET, tblAsset.getGoodsId());
			cacheManager.delete(AssetCache.TBL_ASSET_BY_ID, String.valueOf(tblAsset.getId()));
		}

		recursionFindAssetPage();

	}

	/**
	 * 当前记录的goods_id和parent_id一致则直接取当前ID，否则就继续找parent_id的上级，如果不是根上级则继续向上寻找，从而找到根上级
	 *
	 * @param tblAsset
	 */
	private Long recursionFindRootAssetId(TblAsset tblAsset) {
		String goodsId = tblAsset.getGoodsId();
		String parentId = tblAsset.getParentId();
		if (goodsId.equals(parentId)) {
			return tblAsset.getId();
		}

		LambdaQueryWrapper<TblAsset> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.select(TblAsset::getId, TblAsset::getParentId, TblAsset::getGoodsId, TblAsset::getRootAssetId);
		queryWrapper.eq(TblAsset::getGoodsId, parentId);
		TblAsset tblAssetOne = tblAssetMapper.selectOne(queryWrapper);
		if (tblAssetOne == null || tblAssetOne.getId() == null) {
			return tblAsset.getId();
		}

		Long rootAssetId = recursionFindRootAssetId(tblAssetOne);

		return rootAssetId;
	}

}
